<?php
/**
* 知识星球模型
*/
class Lore_model extends MY_Model
{
	function __construct()
	{
		parent::__construct();
	}

	/**
	 * [获取知识列表]
	 * @Author   zt
	 * @DateTime 2018-08-03
	 * @param    [type]     $page [页码]
	 */
	function loreList($page)
	{
		$where = array('del_status' => '0');
		$like = array();

		$list = $this->hw_list('lore', $where, '*', $like, $page);
		$count = $this->hw_countTable('lore', $where, $like);

		if ($list) {
			$return = array(
				'code'  => 1,
				'msg'   => '数据获取成功',
				'list'  => $list,
				'count' => $count,
				);
		} else {
			$return = array(
				'code'  => 0,
				'msg'   => '暂无数据',
				'list'  => array(),
				'count' => $count,
				);
		}
		
		return $return;
	}

	/**
	 * [知识上架下架]
	 * @Author   zt
	 * @DateTime 2018-08-03
	 * @param    [type]     $id   [知识id]
	 * @param    [type]     $type [1上架2下架]
	 */
	function loreRelease($id, $type)
	{
		$where = array('id' => $id);
		$data = array(
			'state' => $type,
			'update_time' => date('Y-m-d H:i:s'),
		);

		$res = $this->hw_Update('lore', $data, $where);

		if ($res) {
			$return = array(
				'code' => 1,
				'msg' => '操作成功',
			);
		} else {
			$return = array(
				'code' => 0,
				'msg' => '操作失败',
			);
		}

		return $return;
	}

	/**
	 * [知识添加]
	 * @Author   zt
	 * @DateTime 2018-08-03
	 * @param    [type]     $post [添加内容]
	 */
	function loreAdd($post)
	{
		if (isset($post['img'])) {
			$post['img'] = implode(',', $post['img']);
		}

		if (isset($post['file'])) {
			$post['file'] = implode(',', $post['file']);
		}

		$res = $this->hw_Insert('lore', $post);

		if ($res) {
			if ($post['txt_type'] == 2) {
				$this->downLoadFile($post['file']);
			}
			$return = array(
				'code' => 1,
				'msg' => '添加成功',
			);
		} else {
			$return = array(
				'code' => '添加失败',
			);
		}

		return $return;
	}

	/**
	 * [获取知识详情]
	 * @Author   zt
	 * @DateTime 2018-08-03
	 * @param    [type]     $id [知识id]
	 */
	function loreFind($id)
	{
		$where = array('id' => $id);

		$find = $this->hw_getRecord('lore', $where, '*');

		if ($find) {
			$img = $file = $cover = array();

			if ($find['txt_type'] == 0) {
				$img = $this->hw_getList_in('file_library', 'id', explode(',', $find['img']), '*');
			} elseif ($find['txt_type'] == 1) {
				$cover = $this->hw_getRecord('file_library', array('id' => $find['cover']), '*');
			} elseif ($find['txt_type'] == 2) {
				$file = $this->hw_getList_in('file_library', 'id', explode(',', $find['file']), '*');
			}

			$return = array(
				'code' => 1,
				'msg' => '数据获取成功',
				'find' => $find,
				'img' => $img,
				'file' => $file,
				'cover' => $cover,
			);
		} else {
			$return = array(
				'code' => 0,
				'msg' => '数据获取失败',
			);
		}

		return $return;
	}

	/**
	 * [知识修改]
	 * @Author   zt
	 * @DateTime 2018-08-17
	 * @param    [type]     $id   [description]
	 * @param    [type]     $post [description]
	 * @return   [type]           [description]
	 */
	function loreUpdate($id, $post)
	{
		$where = array('id' => $id);

		if (isset($post['img'])) {
			$post['img'] = implode(',', $post['img']);
		}

		if (isset($post['file'])) {
			$post['file'] = implode(',', $post['file']);
		}

		$post['update_time'] = date('Y-m-d H:i:s');

		$res = $this->hw_Update('lore', $post, $where);

		if ($res) {
			if ($post['txt_type'] == 2) {
				$this->downLoadFile($post['file']);
			}
			$return = array(
				'code' => 1,
				'msg' => '编辑成功',
			);
		} else {
			$return = array(
				'code' => 0,
				'msg' => '编辑失败',
			);
		}

		return $return;
	}

	/**
	 * [下载文件到本地]
	 * @Author   zt
	 * @DateTime 2018-08-17
	 * @param    [type]     $id   [文件id]
	 */
	function downLoadFile($id)
	{
		$file = $this->hw_getRecord('file_library', array('id' => $id), '*');
		
		// 判断文件夹是否存在,不存在则生成文件夹
	    if (!is_dir('static/file/')) {
			mkdir('static/file/', 0777, true);
		}

		$filename = "static/file/".md5($file['create_time']).'.'.$file['type'];

		// 判断文件是否存在
		if (!file_exists($filename)) {
		    file_put_contents($filename, file_get_contents($file['url']));
		}
	}
}